package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.utils.DateUtils;
import com.runvp.common.utils.ShiroUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.biz.service.IBizDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 * 
 * @author runvp
 * @date 2022-09-18
 */
@Controller
@RequestMapping("/biz/device")
public class BizDeviceController extends BaseController {
    private String prefix = "biz/device";

    @Autowired
    private IBizDeviceService bizDeviceService;

    @RequiresPermissions("biz:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备列表
     */
    @RequiresPermissions("biz:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizDevice bizDevice) {
        startPage();
        bizDevice.setTenantId(getUserIdFilterAdmin());
        List<BizDevice> list = bizDeviceService.selectBizDeviceList(bizDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @RequiresPermissions("biz:device:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizDevice bizDevice) {
        bizDevice.setTenantId(getUserIdFilterAdmin());
        List<BizDevice> list = bizDeviceService.selectBizDeviceList(bizDevice);
        ExcelUtil<BizDevice> util = new ExcelUtil<BizDevice>(BizDevice.class);
        return util.exportExcel(list, "设备数据");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @RequiresPermissions("biz:device:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizDevice bizDevice) {
        bizDevice.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(bizDeviceService.insertBizDevice(bizDevice));
    }

    /**
     * 修改设备
     */
    @RequiresPermissions("biz:device:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizDevice bizDevice = bizDeviceService.selectBizDeviceById(id);
        mmap.put("bizDevice", bizDevice);
        return prefix + "/edit";
    }
    /**
     * 设备详情
     */
    @RequiresPermissions("biz:device:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        BizDevice bizDevice = bizDeviceService.selectBizDeviceById(id);
        mmap.put("bizDevice", bizDevice);
        return prefix + "/detail";
    }
    /**
     * 设备重启
     */
    @RequiresPermissions("biz:device:restart")
    @Log(title = "设备重启", businessType = BusinessType.OTHER)
    @PostMapping( "/restart")
    @ResponseBody
    public AjaxResult restart(String ids) {
        return toAjax(bizDeviceService.restart(ids));
    }
    /**
     * 修改保存设备
     */
    @RequiresPermissions("biz:device:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizDevice bizDevice) {
        bizDevice.setUpdateBy(ShiroUtils.getLoginName());
        bizDevice.setUpdateTime(DateUtils.getNowDate());
//        bizDevice.setQrCode(BuildQRCode.createCode(bizDevice.getDeviceCode(), RunvpConfig.getPreview()+"/wechat.html?baseSn="+bizDevice.getDeviceCode()));
        return toAjax(bizDeviceService.updateBizDevice(bizDevice));
    }

    /**
     * 删除设备
     */
    @RequiresPermissions("biz:device:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizDeviceService.deleteBizDeviceByIds(ids));
    }
}
